{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "\n",
    "from torch.nn import Parameter\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import make_classification\n",
    "from torch.autograd import Variable\n",
    "\n",
    "get_ipython().run_line_magic('pylab', 'inline')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ModuleWrapper(nn.Module):\n",
    "    \"\"\"Wrapper for nn.Module with support for arbitrary flags and a universal forward pass\"\"\"\n",
    "\n",
    "    def __init__(self):\n",
    "        super(ModuleWrapper, self).__init__()\n",
    "\n",
    "    def set_flag(self, flag_name, value):\n",
    "        setattr(self, flag_name, value)\n",
    "        for m in self.children():\n",
    "            if hasattr(m, 'set_flag'):\n",
    "                m.set_flag(flag_name, value)\n",
    "\n",
    "    def forward(self, x):\n",
    "        for module in self.children():\n",
    "            x = module(x)\n",
    "        return x\n",
    "\n",
    "\n",
    "class LinearVariance(ModuleWrapper):\n",
    "    def __init__(self, in_features, out_features, bias=True):\n",
    "        super(LinearVariance, self).__init__()\n",
    "        self.in_features = in_features\n",
    "        self.out_features = out_features\n",
    "        self.sigma = Parameter(torch.Tensor(out_features, in_features))\n",
    "        if bias:\n",
    "            self.bias = Parameter(torch.Tensor(1, out_features))\n",
    "        else:\n",
    "            self.register_parameter('bias', None)\n",
    "        self.reset_parameters()\n",
    "\n",
    "    def reset_parameters(self):\n",
    "        stdv = 1. / math.sqrt(self.sigma.size(1))\n",
    "        self.sigma.data.uniform_(-stdv, stdv)\n",
    "        if self.bias is not None:\n",
    "            self.bias.data.zero_()\n",
    "\n",
    "    def forward(self, x):\n",
    "        lrt_mean = self.bias\n",
    "        lrt_std = torch.sqrt_(1e-16 + F.linear(x * x, self.sigma * self.sigma))\n",
    "        eps = Variable(lrt_std.data.new(lrt_std.size()).normal_())\n",
    "        return lrt_mean + eps * lrt_std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(means, variance=[[1, 0], [0, 1]], n=500):\n",
    "    xs, ys = [], []\n",
    "    \n",
    "    for c, mean in enumerate(means):\n",
    "        x, y = np.random.multivariate_normal(mean, variance, n).T\n",
    "        data, labels = np.array(list(zip(x, y))), np.zeros(n)+c\n",
    "        xs.append(data)\n",
    "        ys.append(labels)\n",
    "        \n",
    "    X, y = np.vstack(xs).astype(float32),  np.hstack(ys).astype(long)\n",
    "\n",
    "    return X, y\n",
    "\n",
    "Xtr, ytr = get_data([[3, 3], [3, 10], [10, 3], [10, 10]])\n",
    "Xte, yte = get_data([[3, 3], [3, 10], [10, 3], [10, 10]])\n",
    "pylab.scatter(Xtr[:, 0], Xtr[:, 1], c=ytr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = torch.nn.Sequential(\n",
    "  torch.nn.Linear(2, 100),\n",
    "  torch.nn.LeakyReLU(),\n",
    "    \n",
    "  torch.nn.Linear(100, 100),\n",
    "  torch.nn.LeakyReLU(),\n",
    "    \n",
    "  torch.nn.Linear(100, 100),\n",
    "  torch.nn.LeakyReLU(),\n",
    "    \n",
    "  torch.nn.Linear(100, 100),\n",
    "  torch.nn.LeakyReLU(),\n",
    "  \n",
    "  LinearVariance(100, 2),\n",
    "  torch.nn.LeakyReLU(),\n",
    "  \n",
    "  torch.nn.Linear(2, 100),\n",
    "  torch.nn.LeakyReLU(),\n",
    "  \n",
    "  torch.nn.Linear(100, 4),\n",
    "  torch.nn.Softmax()\n",
    ")\n",
    "\n",
    "loss_fn = torch.nn.CrossEntropyLoss(size_average=True)\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(21000):\n",
    "    y_pred = model(Variable(torch.from_numpy(Xtr)))\n",
    "    y_true = Variable(torch.from_numpy(ytr)).long()\n",
    "    loss = loss_fn(y_pred, y_true)\n",
    "    \n",
    "    optimizer.zero_grad()\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "\n",
    "    if t % 1000 == 0:\n",
    "        y_test_sam = model(Variable(torch.from_numpy(Xte)))\n",
    "        \n",
    "        y_test_ens = model(Variable(torch.from_numpy(Xte)))\n",
    "        for i in range(100):\n",
    "            y_test_ens += model(Variable(torch.from_numpy(Xte))) \n",
    "        y_test_ens /= 101.\n",
    "        \n",
    "        loss = loss.item()\n",
    "        acc_sam =  np.mean(y_test_sam.argmax(1).numpy() == yte)\n",
    "        acc_ens = np.mean(y_test_ens.argmax(1).numpy() == yte)\n",
    "        \n",
    "        print('iter %s:' % t, \n",
    "              'loss_train = %.3f' % loss, \n",
    "              'acc_test_one_sampl = %.3f' % acc_sam,\n",
    "              'acc_test_ens = %.3f' % acc_ens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
